package com.itheima.controller;

import com.itheima.aop.annotation.LogOperation;
import com.itheima.aop.annotation.Mylog;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/depts")//如果类上添加了@RequestMapping，真正的请求路径就是类上的路径加上方法上的路径

public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * Springboot封装了restful风格的请求方式
     *
     * @return
     * @GetMapping => @RequestMapping(method = RequestMethod.GET)
     * @PostMapping => @RequestMapping(method = RequestMethod.POST)
     * @PutMapping => @RequestMapping(method = RequestMethod.PUT)
     * @DeleteMapping => @RequestMapping(method = RequestMethod.DELETE)
     */
    //限制访请求方式
    //@RequestMapping(value = "/depts",method = {RequestMethod.GET})//接收get请求

    /**
     * 查询所有部门
     * @return
     * 241325仨而且
     *
     */
    @GetMapping()
    @Mylog
    public Result list() {
        log.info("查询所有部门");
        List<Dept> list = deptService.selectAll();
        return Result.success(list);
    }

    /**
     * 删除部门
     * 我删删删
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteById(Integer id) {
        log.info("根据id删除部门");
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping
    @LogOperation
    public Result add(@RequestBody Dept dept){
        log.info("新增部门");
        deptService.add(dept);
        return Result.success();
    }
    /**
     * 根据id查询部门
     * /{id}路径中的id值，通过@PathVariable注解获取
     * 如果参数名和方法的形参名一致，可以使用@PathVariable注解
     * @param id
     * @return
     */
    @GetMapping("/{id}")//
    public Result selectById(@PathVariable Integer id){
        log.info("根据id查询部门");
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }
    /**
     * 修改部门
     * @param dept
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门");
        return Result.success(deptService.update(dept));
    }
}
